package com.miya.demo.common.util.excel;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;

import java.util.List;

public class HighlightCellWriteHandler implements CellWriteHandler {

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                 Head head, Integer integer, Integer integer1, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell,
                                Head head, Integer integer, Boolean isHead) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<CellData> list, Cell cell, Head head, Integer integer, Boolean isHead) {
        if (isHead) {
            List<String> headNameList = head.getHeadNameList();
            for (String value : headNameList) {
                Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
                Font ftRed = workbook.createFont();
                ftRed.setColor(IndexedColors.RED.index);
                XSSFRichTextString textString = new XSSFRichTextString(value);
                if (value.indexOf("（必填）")!=-1){
                    textString.applyFont(value.indexOf("（必填）"), value.indexOf("（必填）")+"（必填）".length(), ftRed);
                    cell.setCellValue(textString);
                }
            }

        }
    }


}
